<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 用户登录
*/
class LoginController extends Controller
{
	public function index()
	{
		$this->display();
	}
	//生成验证码
	public function verify()
	{
		$Verify = new \Think\Verify();
		$Verify->expire = 120;
		$Verify->useImgBg = true;
		$Verify->fontSize = 20;
		$Verify->length = 4;
		$Verify->imageW = 142;
		$Verify->imageH = 40;
		$Verify->useNoise = false;
		$Verify->bg = array(rand(0, 255), rand(0, 255), rand(0, 255));
		$Verify->entry();
	}
	//登录检查
	public function checklogin()
	{
		if (IS_POST) {
			$Admin = D('Admin');
			if (!$Admin->create(I('post.'),4)) {
				alertMes($Admin->getError(), '/admin/login');
			} else {
				$arr['username'] = I('post.username');
				$arr['active'] = 1;
				$res = $Admin->where($arr)->field('id,username,password,key')->find();
				if ($res) {
					$password = md5(md5(I('post.password')).$res['key']);
					if ($password == $res['password']) {
						session('admin.id', $res['id']);
						session('admin.username', $res['username']);
						$time['last_logdate'] = time();
						$Admin->where('id='.$res['id'])->save($time);
						alertMes('登录成功', '/admin');
					} else {
						alertMes('用户密码错误', '/admin/login');
					}
				} else {
					alertMes('用户名不存在或用户未激活', '/admin/login');
				}
			}
		} else {
			$this->assign('jumpUrl','/'.MODULE_NAME.'/login');
			$this->error('请先登录');
		}
	}
	//用户登出
	public function logout()
	{	
		$a=session('admin.id');
		if (!empty($a)) {
			session(null);
			$this->assign('jumpUrl','/'.MODULE_NAME.'/login');
			$this->success('登出成功');
		} else {
			$this->assign('jumpUrl','/'.MODULE_NAME.'/login');
			$this->error('未登录');
		}
	}
}